Scroll Progress Bar

Virtual Function

In C++, a virtual function is a member function of a class that is declared as virtual in the base class and it can be overridden by derived classes. Virtual functions enable polymorphism and late binding, allowing the correct function implementation to be selected at runtime based on the actual type of the object, rather than the type of the pointer or reference to the object. Here's how virtual functions work and how to use them:

1. Declaring a Virtual Function:

To declare a virtual function in a base class, use the virtual keyword before the function's return type in the base class definition. For example:

Program:

class Base {
public:
    virtual void myFunction() {
        // Base class implementation
    }
};
2. Overriding a Virtual Function:

Derived classes it can override a virtual function by declaring a function with the same name, parameter list, and return type as the virtual function in the base class. The override keyword is used to indicate that a function is intended to override a virtual function:

Program:

class Derived : public Base {
public:
    void myFunction() override {
        // Derived class implementation
    }
};
3. Late Binding (Dynamic Binding):

When call a virtual function on an object through a pointer or reference to the base class, C++ uses late binding (dynamic binding or runtime polymorphism) to determine which version of the function to execute. Late binding allows the program to select the appropriate function implementation based on the actual type of the object, rather than the declared type of the pointer or reference.

Here's an example illustrating the use of virtual functions:

Program:

#include <iostream>

class Shape {
public:
    virtual void display() {
        std::cout << "Shape" << std::endl;
    }
};

class Circle : public Shape {
public:
    void display() override {
        std::cout << "Circle" << std::endl;
    }
};

class Rectangle : public Shape {
public:
    void display() override {
        std::cout << "Rectangle" << std::endl;
    }
};

int main() {
    Shape* shapePtr = nullptr;
    Circle circle;
    Rectangle rectangle;

    shapePtr = &circle;
    shapePtr->display(); // Calls Circle's display function

    shapePtr = &rectangle;
    shapePtr->display(); // Calls Rectangle's display function

    return 0;
}
In this example:

a base class Shape with a virtual function display.

Two derived classes, Circle and Rectangle, override the display function with their own implementations.

In the main function, we use a pointer of type Shape* to access the display function on objects of both derived classes. Late binding ensures that the appropriate overridden version of display is called based on the actual type of the object pointed to by shapePtr.

Virtual functions are a fundamental concept in C++ inheritance and polymorphism. They enable to create class hierarchies with a common interface in the base class while allowing derived classes to provide specific implementations as needed.


question


answer

question2


answer2